let express = require("express")();
let expressStatic = require("express-static")
let mysql = require("mysql")
let sql= mysql.createConnection({
  host     : 'localhost',
  user     : 'root',
  password : '123456',
  database : 'news',
  timezone:"08:00"
 
});
 sql.connect();
 
express.get("/student",(request,response)=>{
	sql.query(`SELECT * FROM student`,(error,data)=>{
		if(error){
			console.log(error)
			response.send("error")
		}
		else{
			response.send(data)
		}
	})
})

express.get("/deletestudent",(request,response)=>{
	let id = request.query.id
	console.log( request.query)
	sql.query(`DELETE FROM student WHERE id=${id}`,(error,data)=>{
		if(error){
			console.log(error)
			response.send("error")
		}
		else{
			response.send(data)
		}
	})
})

server.get("/joinstudent",(request,response)=>{
	let result = request.query;
	sql.query(`INSERT INTO students (name,age,sex,city,time) VALUES ("${result.name}","${result.age}","${result.sex}","${result.city}","${result.time}")`,(error,data)=>{
		if(error){
			console.log(error)
			response.send("error")
		}
		else{
			response.send("success")
			console.log(`新增学生${result.name}操作成功`)
		}
	})
})
express.use(expressStatic(__dirname + '/static'));
const port = 81;
express.listen(port)
console.log(`runing at ${port}`)